import datetime

from common.DateTimeUtil import get_current_date_YMD, get_current_date_YMDHMS


#sql拼接，deviceid目前设置一套，后续改进优化，point为list
def sqlContact(deviceID,point_value,name):
    if name == 'day_':
        dbTime = (datetime.date.today()).strftime("%Y%m")
    else:
        dbTime=(datetime.date.today()).strftime("%Y%m%d")
    creatTime = ( datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
    index=0
    insql="INSERT IGNORE INTO service_values_"+name+dbTime+" (ID_DEVICE,ID_SERVICE,TAG_NAME,DATE_TIME,PV,MIN,MAX,EU) VALUES"
    result=""
    for k in point_value:
        a = point_value[k]
        start="('"
        end ="', NULL)"
        middle="', '"
        if (index+1)!=len(point_value):
            result = result+start+deviceID+middle+deviceID+"\\\\"+k+middle+k+middle+creatTime+middle+str(point_value[k])+middle+str(point_value[k])+middle+str(point_value[k])+end+","
        else:
            result = result+start+deviceID+middle+deviceID+"\\\\"+k+middle+k+middle+creatTime+middle+str(point_value[k])+middle+str(point_value[k])+middle+str(point_value[k])+end
        index+=1
    return insql+result


#mqtt 消息拼接
def mqttMessageContact(deviceID,point_value):
    creatTime = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
    keys =["deviceId","timestamp","values"]
    value1=[deviceID,creatTime,point_value,]
    mqttData=dict(zip(keys,value1))
    return mqttData

